package console;

import java.util.ArrayList;

/**
 *
 * @author Michael Hanns
 *
 */

public class InputHistoryImp implements InputHistory {


    private ArrayList<String> history;
    private int historyLine;

    public InputHistoryImp() {
        history = new ArrayList<String>();
        historyLine = -1;
    }

    @Override
    public void addLine(String input) {
        history.add(input);
        restartHistory();
    }

    @Override
    public String getLastLine() {
        if(getTotalLines() == 0) {
            return "";
        }

        historyLine--;

        if(historyLine <= -1) {
            restartHistory();
            return "";
        }
        return history.get(historyLine);
    }

    @Override
    public String getNextLine() {
        if(getTotalLines() == 0) {
            return "";
        }

        historyLine++;

        if(historyLine >= getTotalLines()) {
            historyLine = -1;
            return "";
        }
        return history.get(historyLine);
    }

    @Override
    public void restartHistory() {
        historyLine = history.size();
    }

    @Override
    public int getTotalLines() {
        return history.size();
    }



}
